import os
import sys
from pathlib import Path

def create_spec_file():
    """
    创建PyInstaller spec文件
    """
    spec_content = '''# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(
    ['quiz_gui.py'],
    pathex=[],
    binaries=[],
    datas=[('parsed_questions/**/*', 'parsed_questions')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='商业察言观色训练系统',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    icon='icon.ico'  # 如果有图标文件可以取消注释
)
'''
    
    with open('quiz_gui.spec', 'w', encoding='utf-8') as f:
        f.write(spec_content)
    
    print("已创建 quiz_gui.spec 文件")

def main():
    # 检查是否安装了PyInstaller
    try:
        import PyInstaller
    except ImportError:
        print("未检测到 PyInstaller，正在安装...")
        os.system(f"{sys.executable} -m pip install pyinstaller")
    
    # 创建spec文件
    create_spec_file()
    
    # 运行PyInstaller
    print("正在打包应用程序...")
    os.system("pyinstaller quiz_gui.spec")
    
    print("打包完成！可执行文件位于 dist/ 目录中")

if __name__ == "__main__":
    main()